$(document).ready(init);

/**
 * 查询用户信息
 */
function init() {
	var queryParams = {
	    
	};
	queryUser(queryParams, true);
	
	$('#searchButton').click(searchByName);
}

function searchByName() {
	var queryParams = {
		'employee.empName' : $.trim($('#empName').val()),
		'employee.loginAccount' : $.trim($('#loginAccount').val())
	};
	
	queryUser(queryParams, true);
}

/**
 * 初始化注册信息
 */
function queryUser(queryParams, flag) {
		$('#reviewRegister').datagrid({
	    title:'',
	    iconCls:'icon-save',
	    singleSelect:true,
	    url:'iemployee-admin!queryEmployeeList.action',
	    idField: 'empID',
	    queryParams :queryParams,
	    columns:[[
	        {field:'empName',title:'真实姓名',width:fixWidth(0.08)},
	        {field:'loginAccount',title:'登录名',width:fixWidth(0.08)},
	        {field:'linkPhone',title:'联系电话',width:fixWidth(0.1)},
	        {field:'email',title:'邮箱',width:fixWidth(0.1)},
	        {field:'userType',title:'身份',width:fixWidth(0.1),
	        	formatter : function(value, rowData, rowIndex) {//0:院内、1:校内、2:校外
	        	    	var x = '<a href="#" onclick="showDetail('+ rowIndex + ',\'' + value +'\')">详情&nbsp;&nbsp;</a> ';
	        	    	
						if (value == '0') {
							return '院内';
				 		} else if (value == '1'){
				 			return '校内';
						} else if (value == '2') {
							return '校外';
						}
					}
	        },
	        {field:'deptName', title:'课题组',width:fixWidth(0.1), hidden : flag},
	        {field:'workEntity', title:'工作单位', width:fixWidth(0.1)},
	        {field:'empType', title:'用户类型', width:fixWidth(0.1),
	        	formatter:function(value, rowData, rowIndex) {
						var content ='';
						if ('1' == value) {
							content = '注册用户';
						} else if ('2' == value) {
							content = '正式用户';
						} else if ('3' == value) {
							content = '课题组长';
						} else if ('4' == value) {
							content = '仪器管理员';
						} else if ('5' == value) {
							content = '实验室管理员';
						} else if ('6' == value) {
							content = '系统管理员';
						} else {
							content = '';
						}
						
						return content;
					}
	        },
	        {field:'stopState', title:'用户状态', width:fixWidth(0.1),hidden:false,
	        	formatter:function(value, rowData, rowIndex) {
						var content ='正常';
						if (0 == value) {
							content = '停用';
						}
						return content;
					}
	        },
	         {field:'isCheck', title:'是否审核', width:fixWidth(0.1),
	        	formatter:function(value, rowData, rowIndex) {
						var content ='已审核';
						if (0 == value) {
							content = '待审核';
						}
						return content;
					}
	        },
	        {field:'loginPwd', title:'m',width:fixWidth(0.1), hidden : true},
	        {field:'empID',title:'操作',width:fixWidth(0.1),align:'center',
	        	    formatter : function(value, row, index) {
						if (row.editing || row.loginPwd == 'password') {
							return '已经初始化';
				 		} else {
							var e = '<a href="#" style="color:red" onclick="initSec('+ index + ','  + value + ')">初始化</a> ';
							return e;
						}
					}
			}
	    ]],
	     onBeforeEdit:function(index,row){
				row.editing = true;
				$('#reviewRegister').datagrid('refreshRow', index);
		},
		onAfterEdit:function(index,row){
				row.editing = false;
				$('#reviewRegister').datagrid('refreshRow', index);
		},
		onCancelEdit:function(index,row){
				row.editing = false;
				$('#reviewRegister').datagrid('refreshRow', index);
		},
	    pagination:true,
	    rownumbers:true,
	    fitColumns:true
	});
}

function initSec(index, value) 
{
	 $.messager.confirm('确定', '您确定要进行初始化？' ,function(r){
        if (r){
           var params = {
				empID : value
			}
			
			var option = {
		   		type: 'POST',
		   		url: 'iemployee!initSec.action',
		   		data: params,
		   		success : function(msg){
		   			if (msg == 'success') {
		   				$.messager.alert('确定', '恭喜您操作成功', 'info');
		   				
		   				$('#reviewRegister').datagrid('beginEdit', index);
		   			} else {
		   				$.messager.alert('确定', '操作失败，请稍后或刷新页面尝试', 'error');
		   			}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					$.messager.alert('确定', '处理失败：' + errorThrown, 'error')
				}
			}
			
			$.ajax(option); 
        }
     });
}
